<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Files extends CI_Controller {

    function __construct() {
        parent::__construct();

        $this->load->model('Query_builder_model', 'qb');
        $param = array(
            'controller' => $this->controller
        );
        
        $this->load->library('User', $param, 'u');
        
        $this->u->auth();
        
        $this->load->library('Crud', $param, 'cd');
        $this->load->library('Template', $param, 'tpl');
        $this->load->library('File_manipulate', $param, 'fm');
        $this->load->model('Pagination_model', 'pg');
    }

    private $controller = 'files';

    public function index($action='list', $id=0) {

        $res = '';
        $ajax = $this->input->post('ajax');

        switch ($action) {
            case 'add':
                $res = $this->cd->c_add();
                break;
            case 'edit':
                $res = $this->cd->c_edit($id);
                break;
            case 'save':
                $id = $this->input->post('id');
                $id = $this->fm->upload($this->u->id, $id);
                $res = $this->cd->c_save($id);
                break;
            default :
                $res = $this->cd->c_listing($id);
                if ($ajax) {
                    exit(json_encode(array('html' => $res)));
                }

                $this->tpl->set('file_upload', true);
                $uploader = $this->load->view('crud/fileupload', array('controller' => $this->controller, 'user' => $this->u->id), true);
                $this->tpl->initialize(array('uploader' => $uploader));
                break;
        }



        $this->tpl->initialize(array('current_page' => $res));
        echo $this->tpl->render();
    }

    function sort($field, $order='ASC') {
        $this->cd->set_sort(array('field' => $field, 'order' => $order));
        $this->index();
    }

}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */